package fr.umlv.projet.android.subject;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.MeasureSpec;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import fr.umlv.projet.android.andex.R;
import fr.umlv.projet.android.parser.jaxb.Content;
import fr.umlv.projet.android.parser.jaxb.Node;
import fr.umlv.projet.android.saisi.SaveResponse;
import fr.umlv.projet.android.saisi.audio.AudioView;
import fr.umlv.projet.android.saisi.camera.TakePicture;
import fr.umlv.projet.android.saisi.other.TakeString;
import fr.umlv.projet.android.saisi.tactile.DrawTactile;

public class ItemActivity extends Activity {

	protected static final int RESULT_PREVIOUS = 17341;
	protected static final int RESULT_NEXT = 17342;
	protected static final int RESULT_VALIDATE = 17343;
	private List<SaveResponse> reponse = new ArrayList<SaveResponse>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_item);

		Node node = (Node) getIntent().getExtras().getSerializable("node");
		Boolean buttonPreviousInvisible = getIntent().getExtras().getBoolean(
				"buttonPreviousInvisible");
		Boolean buttonNextInvisible = getIntent().getExtras().getBoolean(
				"buttonNextInvisible");
		final String examName = getIntent().getExtras().getString("examName");
		final String itemTitle = node.getTitle().trim();

		String limitedTime = node.getItem().getLimitedTime();
		if (limitedTime != null && limitedTime.equals("true")) {
			// ameliroration : temps limite pour repondre
		}

		final TextView tv1 = (TextView) findViewById(R.id.textViewItemTitle);
		tv1.setText(itemTitle);

		final TextView tv2 = (TextView) findViewById(R.id.textViewItemQuestion);
		StringBuilder sb = new StringBuilder();
		for (Content c : node.getItem().getQuestion().getContent()) {
			String type = c.getType();
			Log.d(getLocalClassName(), "question type: " + type);
			if (type.equals("text")) {
				sb.append(c.getValue()).append("\n");
			} else if (type.equals("image")) {
				// ameliroration : afficher image
				// sb.append("todo-image:").append(c.getValue()).append("\n");
			} else if (type.equals("sound")) {
				// ameliroration : jouer son
				// sb.append("todo-sound:").append(c.getValue()).append("\n");
			} else if (type.equals("video")) {
				// ameliroration : afficher video
				// sb.append("todo-video:").append(c.getValue()).append("\n");
			}
		}
		tv2.setText(sb);

		final TextView tv3 = (TextView) findViewById(R.id.textViewItemAnswer);
		tv3.setText("");
		
		RelativeLayout rl = (RelativeLayout) findViewById(R.id.relativeLayoutItem);
		rl.measure(MeasureSpec.UNSPECIFIED,MeasureSpec.UNSPECIFIED);
		int width = rl.getMeasuredWidth();
		Log.d(getLocalClassName(), "relativeLayout width: " + width);
		int size = (int) (width * 0.92);
		LinearLayout ll = (LinearLayout) findViewById(R.id.linearView);
		final String path = node.getPath();
		if (node.getItem().getExpectedAnswer() != null) {
			if(!node.getItem().getExpectedAnswer().getType().isEmpty()){
				tv3.setText("Answer :");
				tv3.setTextSize(18);
			}
			int draw=0, sound=0, picture=0, text=0;
			for (String s : node.getItem().getExpectedAnswer().getType()) {
				Log.d(getLocalClassName(), "expected answer: " + s);
				TextView type = new TextView(this);
				
				if (s.equals("text")) {
					type.setText("- Text:");
					ll.addView(type);
					TakeString et = new TakeString(this);
					et.setBackgroundColor(getResources().getColor(R.color.white));
					ll.addView(et);
					et.setDestFile(s+(text==0?"":text));
					reponse.add(et);
					text++;
				} else if (s.equals("draw")) {
					type.setText("- Draw:");
					ll.addView(type);
					DrawTactile dt = new DrawTactile(this);
					ll.addView(dt, size, size);
					dt.setDestFile(s+(draw==0?"":sound));
					reponse.add(dt);
					draw++;
				} else if (s.equals("picture")) {
					type.setText("- Picture:");
					ll.addView(type);
					TakePicture tp = new TakePicture(this);
					double ratio = tp.getRatio();
					Log.d(getLocalClassName(), "ratio camera: " + ratio);
					ll.addView(tp, size, (int) (size/ratio));
					tp.setDestFile(s+(picture==0?"":sound));
					reponse.add(tp);
					picture++;
				} else if (s.equals("sound")) {
					type.setText("- Sound:");
					ll.addView(type);
					AudioView av = new AudioView(this);
					ll.addView(av, size, size);
					av.setDestFile(s+(sound==0?"":sound));
					reponse.add(av);
					sound++;
				}
			}
		}

		final Button previousButton = (Button) findViewById(R.id.buttonItemPrevious);
		previousButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				setResult(RESULT_PREVIOUS);
				finish();
			}
		});
		if (buttonPreviousInvisible) {
			previousButton.setVisibility(View.GONE);
		}

		final Button nextButton = (Button) findViewById(R.id.buttonItemNext);
		nextButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				setResult(RESULT_NEXT);
				finish();
			}
		});
		if (buttonNextInvisible) {
			nextButton.setVisibility(View.GONE);
		}

		final Button backButton = (Button) findViewById(R.id.buttonItemBack);
		backButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});
		final Button validateButton = (Button) findViewById(R.id.buttonItemValidate);
		validateButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				for(SaveResponse save : reponse){
					save.setPathFile("/Andex/"+examName+"/"+path+"/");
					save.save();
				}
				setResult(RESULT_VALIDATE);
				finish();
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.item, menu);
		return true;
	}

}
